feat: Adaptive Volatility-Aware Recurring Deposit (Smart DCA) Engine - #386
Open
dotunv wants to merge 1 commit into
Open
feat: Adaptive Volatility-Aware Recurring Deposit (Smart DCA) Engine#386dotunv wants to merge 1 commit into
dotunv wants to merge 1 commit into
Conversation
…eurowealth#311) Upgrade recurring deposits from fixed-amount cron to an adaptive, portfolio-aware investment engine with transparent preview and honest failure semantics. Schema: - Extend RecurringDepositPlan with: policy (FIXED/ADAPTIVE), catchUpMode, pauseOnDrawdownPct, doubleOnDrawdown, accumulatedRuns, consecutiveFailures, autoPauseReason, allocationMap - Add RecurringDepositRun model for per-run ledger (baseline vs applied amount, regime snapshot, reasoning, allocation legs) Pure Policy Module (src/deposits/smartDcaPolicy.ts): - Zero-I/O, unit-testable contribution sizing - Volatility regime classification (HIGH/NORMAL/LOW) from trailing data - Bounded regime scaling with floor/ceiling (configurable fractions) - Drawdown pause/double logic (reuses alertEvaluator pattern) - Multi-protocol allocation splitting - Catch-up state machine (RETRY/SKIP/ACCUMULATE) - Auto-pause after consecutive failures - FIXED plans: byte-for-byte identical to legacy (backward compatible) Preview Service (src/deposits/preview.ts): - Deterministic simulation of next N runs under plan policy - Explicitly labeled as simulation, not a guarantee - Shows regime assumptions, drawdown state, allocation math Scheduler (src/jobs/recurringDeposits.ts): - Resolves effective strategy config (own vs. followed) - Loads regime + drawdown data for ADAPTIVE plans - Creates run ledger row for every execution attempt - Auto-pause with user-visible reason after consecutive failures - RESET consecutiveFailures on success Validators: extended with policy, catchUpMode, allocationMap, preview/run query schemas. Routes: POST create + PATCH update support new fields; GET /preview (registered before /:id); GET /:id/runs (paginated run ledger). Closes Neurowealth#311 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Upgrades recurring deposits from a fixed-amount cron job into an adaptive, portfolio-aware investment engine with transparent preview and honest failure semantics.
Closes #311
What's New
Pure Policy Module (
src/deposits/smartDcaPolicy.ts)alertEvaluator.computeDrawdownPercentPreview Endpoint (
GET /api/v1/deposit/recurring/preview)Run Ledger (
GET /api/v1/deposit/recurring/:id/runs)Schema Extensions
RecurringDepositPlan: policy, catchUpMode, pauseOnDrawdownPct, doubleOnDrawdown, accumulatedRuns, consecutiveFailures, autoPauseReason, allocationMapRecurringDepositRunmodel for the per-run audit trailContributionPolicy,CatchUpMode,RecurringDepositRunStatusFiles Changed
prisma/schema.prismaprisma/migrations/20260828160000_smart_dca_engine/src/deposits/smartDcaPolicy.tssrc/deposits/preview.tssrc/jobs/recurringDeposits.tssrc/routes/recurring-deposits.tssrc/validators/recurring-deposit-validators.tstests/unit/deposits/smartDcaPolicy.test.tsAcceptance Criteria Checklist
Design Decisions
FIXED backward compatibility: The policy module is a no-op for FIXED plans — same amount, no regime check, no drawdown pause. Existing plans are completely unaffected.
Regime classification: Uses the "latest value relative to its trailing range" approach — simple, documented, and auditable. Not a forecast. The regime snapshot is stored with every run for full auditability.
Drawdown detection: Reuses the same peak-tracking pattern from
alertRules.ts(computeDrawdownPercent+rollingPeak), so the platform has one consistent definition of drawdown.Auto-pause backoff: After 5 consecutive failures, the plan auto-pauses with a user-visible reason. This mirrors the retriable-failure philosophy of
referralPayout.tsandfiatReconciliation.ts.Run ledger vs. AgentLog: The run ledger is the policy audit trail (what the engine decided and why). AgentLog remains the deposit audit trail (what happened on-chain). Two separate concerns, two separate records.
Testing
🤖 Generated with Codebuff
Co-Authored-By: Codebuff noreply@codebuff.com